package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zcx.helper.secret.SecretAESDESede;
import com.zcx.helper.util.UtilMD5;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.utils.Md5Utils;
import longcaisuyun.longcai.com.utils.TimeCount;
import longcaisuyun.longcai.com.utils.Tools;
import longcaisuyun.longcai.com.utils.UtilsJz;


public class ForgetActivity extends Activity implements View.OnClickListener {

    EditText et_phone, et_num, et_pass, et_passagain;
    Button bt_next;
    String code = "";
    TimeCount timeCount;
    Button bt_ok;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        View view = getWindow().getDecorView();
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
    }

    private void ViewInit() {
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_num = (EditText) findViewById(R.id.et_num);
        et_pass = (EditText) findViewById(R.id.et_pass);
        et_passagain = (EditText) findViewById(R.id.et_passagain);
        bt_next = (Button) findViewById(R.id.bt_next);
        bt_next.setOnClickListener(this);
        bt_ok = (Button) findViewById(R.id.bt_ok);
        bt_ok.setOnClickListener(this);
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_next:
                if (!TextUtils.isEmpty(et_phone.getText().toString())) {
                    if (Tools.isMobile(et_phone.getText().toString())) {
                        bt_next.setClickable(false);
                        httpPost(et_phone.getText().toString().trim(), "2");
                        bt_next.setClickable(true);

                    } else {
                        Toast.makeText(ForgetActivity.this, "输入的手机号码格式错误，请检查后重新输入", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(ForgetActivity.this, "您还未输入手机号码", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.bt_ok:
                if (TextUtils.isEmpty(et_phone.getText().toString())){
                    Toast.makeText(ForgetActivity.this, "您还未输入手机号码", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(et_num.getText().toString())) {
                    Toast.makeText(ForgetActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                }else if (!et_num.getText().toString().equals(code)) {
                    Toast.makeText(ForgetActivity.this, "验证码输入有误，请检查后重新输入", Toast.LENGTH_SHORT).show();

                }else if (et_pass.getText().toString().length()<6||et_pass.getText().toString().length()>16||et_passagain.getText().toString().length()<6||
                    et_passagain.getText().toString().length()>16){
                    Toast.makeText(ForgetActivity.this, "请输入6-16位密码", Toast.LENGTH_SHORT).show();
                }else{
                    switch (UtilsJz.isok(et_pass, et_passagain)) {
                        case 0:
                            httpPost_ok();
                            break;
                        case 1:
//                            Toast.makeText(ForgetActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(ForgetActivity.this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
                break;
        }
    }

    private void httpPost(String mobile, String action) {
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp = new FinalHttp();
        AjaxParams params = new AjaxParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", mobile);
            jsonObject.put("action", action);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finalHttp.post(publicUrl.mobilecode, params, new AjaxCallBack<String>() {
            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(ForgetActivity.this, finalHttp, publicUrl.mobilecode);
            }

            public void onSuccess(String s) {
                super.onSuccess(s);
                try {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(secretAESDESede.decrypt(s));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String Message = jsonObject.getString("message");
                    if (Message.equals("1")) {
                        timeCount = new TimeCount(60000, 1000, bt_next);
                        timeCount.start();
                        code = jsonObject.getString("code");
                        Log.e("code", code);
                        Toast.makeText(ForgetActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                    }
                    if (Message.equals("2")) {
                        Toast.makeText(ForgetActivity.this, "该手机号码不存在", Toast.LENGTH_SHORT).show();
                    }
                    if (Message.equals("0")) {
                        Toast.makeText(ForgetActivity.this, "获取验证码失败，请重新获取", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
    }

    private void httpPost_ok() {
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp = new FinalHttp();
        AjaxParams params = new AjaxParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", et_phone.getText().toString());
            jsonObject.put("newpwd", UtilMD5.MD5EncodeCount(et_pass.getText().toString().trim(),"",2));
            System.out.println(et_pass.getText().toString() + ":**********" + Md5Utils.encode(Md5Utils.encode(et_pass.getText().toString().trim())));
            jsonObject.put("action", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finalHttp.post(publicUrl.wangji, params, new AjaxCallBack<String>() {
            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(ForgetActivity.this, finalHttp, publicUrl.mobilecode);
            }

            public void onSuccess(String s) {
                super.onSuccess(s);
                try {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(secretAESDESede.decrypt(s));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String message = jsonObject.getString("message");
                    if (message.equals("0")) {
                        Toast.makeText(ForgetActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                    }
                    if (message.equals("1")) {
                        Toast.makeText(ForgetActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    if (message.equals("2")) {
                        Toast.makeText(ForgetActivity.this, "手机号码不存在，请检查后重新尝试", Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
    }

    public int isok() {
        if (TextUtils.isEmpty(et_pass.getText().toString().trim()) || TextUtils.isEmpty(et_passagain.getText().toString().trim())) {
            return 1;
        } else if (!et_pass.getText().toString().equals(et_passagain.getText().toString())) {
            return 2;
        } else {
            return 0;
        }
    }

}

